/*
 *  Copyright (C) 2011 Jaime Pavlich-Mariscal
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package cl.ucn.disc.biblio.refcluster;

import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Jaime Pavlich
 */
public class ReferenceMatcherJFrame extends javax.swing.JFrame {

    private JFileChooser fileChooser = new JFileChooser();
    private JFileChooser folderChooser = new JFileChooser();
    private File wosDBFolder;
    private File clusterFile;
    private File newDBFolder;

    /** Creates new form ReferenceMatcherJFrame */
    public ReferenceMatcherJFrame() {
        try {
            initComponents();
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ReferenceMatcherJFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ReferenceMatcherJFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ReferenceMatcherJFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(ReferenceMatcherJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextFieldBiblioDBFolder = new javax.swing.JTextField();
        jButtonBrowseDBFolder = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldClusterFilename = new javax.swing.JTextField();
        jButtonBrowseClusterFile = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldNewDBFolder = new javax.swing.JTextField();
        jButtonBrowseNewDBFolder = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jButtonGenerateClusterFile = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jButtonGenerateNewDB = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Reference Clusterer");

        jLabel1.setText("Bibliographic database folder:  ");

        jTextFieldBiblioDBFolder.setText("D:\\DB");
        jTextFieldBiblioDBFolder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldBiblioDBFolderActionPerformed(evt);
            }
        });

        jButtonBrowseDBFolder.setText("Browse");
        jButtonBrowseDBFolder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBrowseDBFolderActionPerformed(evt);
            }
        });

        jLabel2.setText("Cluster file:");

        jTextFieldClusterFilename.setText("D:\\cluster.txt");

        jButtonBrowseClusterFile.setText("Browse");
        jButtonBrowseClusterFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBrowseClusterFileActionPerformed(evt);
            }
        });

        jLabel3.setText("New bibliographic database folder:");

        jTextFieldNewDBFolder.setText("D:\\NewDB");

        jButtonBrowseNewDBFolder.setText("Browse");
        jButtonBrowseNewDBFolder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBrowseNewDBFolderActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("Step 1: Select folder of where the bibliographic database is stored");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setText("Step 2: Specify the cluster file name");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel6.setText("Step 3: Generate the cluster file");

        jButtonGenerateClusterFile.setText("Generate cluster file");
        jButtonGenerateClusterFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGenerateClusterFileActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel7.setText("Step 4: Select folder for the new database");

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel8.setText("Step 5: Generate the new database");

        jButtonGenerateNewDB.setText("Generate new database");
        jButtonGenerateNewDB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGenerateNewDBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jButtonGenerateClusterFile)
                    .addComponent(jLabel7)
                    .addComponent(jButtonGenerateNewDB)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 366, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextFieldNewDBFolder, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                                .addGap(10, 10, 10)))
                        .addComponent(jButtonBrowseNewDBFolder))
                    .addComponent(jLabel6)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldClusterFilename, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonBrowseClusterFile))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldBiblioDBFolder, javax.swing.GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonBrowseDBFolder)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldBiblioDBFolder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jButtonBrowseDBFolder))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldClusterFilename, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonBrowseClusterFile))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonGenerateClusterFile)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldNewDBFolder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonBrowseNewDBFolder))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonGenerateNewDB)
                .addContainerGap(65, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-668)/2, (screenSize.height-368)/2, 668, 368);
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonBrowseDBFolderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBrowseDBFolderActionPerformed
        int returnVal = folderChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            wosDBFolder = folderChooser.getSelectedFile();
            jTextFieldBiblioDBFolder.setText(wosDBFolder.getAbsolutePath());
        }
    }//GEN-LAST:event_jButtonBrowseDBFolderActionPerformed

    private void jButtonGenerateClusterFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGenerateClusterFileActionPerformed
        try {
            wosDBFolder = new File(jTextFieldBiblioDBFolder.getText());
            clusterFile = new File(jTextFieldClusterFilename.getText());
            ReferenceClusterer rc = new ReferenceClusterer();
            if (wosDBFolder != null && clusterFile != null) {
                rc.createClusterFileFromWoSDB(wosDBFolder, clusterFile);
            } else {
                JOptionPane.showMessageDialog(this, "Select Bibliographic database and cluster file", "Error", JOptionPane.ERROR_MESSAGE);
            }
        } catch (Throwable t) {
            JOptionPane.showMessageDialog(this, t.toString(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jButtonGenerateClusterFileActionPerformed

    private void jButtonBrowseClusterFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBrowseClusterFileActionPerformed
        int returnVal = fileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            clusterFile = fileChooser.getSelectedFile();
            jTextFieldClusterFilename.setText(clusterFile.getAbsolutePath());
        }
    }//GEN-LAST:event_jButtonBrowseClusterFileActionPerformed

    private void jButtonGenerateNewDBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGenerateNewDBActionPerformed
        ReferenceManager rm = new ReferenceManager();
        try {
            wosDBFolder = new File(jTextFieldBiblioDBFolder.getText());
            clusterFile = new File(jTextFieldClusterFilename.getText());
            newDBFolder = new File(jTextFieldNewDBFolder.getText());
            rm.replaceRefsOfFolder(wosDBFolder, newDBFolder, clusterFile);
        } catch (Throwable t) {
            JOptionPane.showMessageDialog(this, t.toString(), "Error", JOptionPane.ERROR_MESSAGE);
//            t.printStackTrace();
        }
    }//GEN-LAST:event_jButtonGenerateNewDBActionPerformed

    private void jButtonBrowseNewDBFolderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBrowseNewDBFolderActionPerformed
        int returnVal = folderChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            newDBFolder = folderChooser.getSelectedFile();
            jTextFieldNewDBFolder.setText(newDBFolder.getAbsolutePath());
        }
    }//GEN-LAST:event_jButtonBrowseNewDBFolderActionPerformed

    private void jTextFieldBiblioDBFolderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldBiblioDBFolderActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldBiblioDBFolderActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ReferenceMatcherJFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBrowseClusterFile;
    private javax.swing.JButton jButtonBrowseDBFolder;
    private javax.swing.JButton jButtonBrowseNewDBFolder;
    private javax.swing.JButton jButtonGenerateClusterFile;
    private javax.swing.JButton jButtonGenerateNewDB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField jTextFieldBiblioDBFolder;
    private javax.swing.JTextField jTextFieldClusterFilename;
    private javax.swing.JTextField jTextFieldNewDBFolder;
    // End of variables declaration//GEN-END:variables
}
